<template>
    <div>
        <Row style="margin:5px 0px 10px 5px" >
            <i-col span="2" >
                <Button  size="default"  type="primary" @click="btn_refresh">刷新</Button>
            </i-col>
		</Row> 
        <i-table size="default"   border stripe  :columns="format" :data="show_list" :height="sharedState.tableHeight" >
        </i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
    </div>
</template>

<script>
import { get_daemonset_list} from  '@/api'
import { k8s_namespace_post} from '@/api/http'
import { get_placement } from '@/common/util'
import store from '@/store'

export default {
    data() {
        return {
            sharedState: store.state,
            format: [
                
                {
                    title: '名字',key: 'name'
                },
                {
                    title: '命名空间',key: 'namespace'
                },
                {
                    title: '标签',key: 'labels',
                    render: (h, params) => {
                        let labels = params.row.labels
                        if(labels == null){
                            return h('p','无')
                        }else {
                            let value = JSON.stringify(labels,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }

                    }
                },
                {
                    title: 'pods',key: 'status',
                },
                {
                    title: '容忍',key: 'tolerations',
                    render : (h,params) => {
                            let tolerations = params.row.tolerations 
                            if (tolerations == null ){
                                return   h('p','无')
                            }else{
                                let value = JSON.stringify(tolerations,undefined,4)
                                let placement = get_placement(params)
                                return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                        h('a', '显示')
                                ]);
                            }
                        }
                },
                {
                    title: '节点亲和性',
                    render : (h,params) => {
                        let affinity  = params.row.affinity
                        if(affinity == null){
                            return h('p','无')
                        }
                        let node_affinity = affinity['node_affinity']
                        if (node_affinity == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(node_affinity,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: 'Pod亲和性',
                    render : (h,params) => {
                        let affinity  = params.row.affinity
                        if(affinity == null){
                            return h('p','无')
                        }
                        let pod_affinity = affinity['pod_affinity']
                        if (pod_affinity == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(pod_affinity,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: 'Pod互斥调度',
                    render : (h,params) => {
                        let affinity  = params.row.affinity
                        if(affinity == null){
                            return h('p','无')
                        }
                        let pod_anti_affinity = affinity['pod_anti_affinity']
                        if (pod_anti_affinity == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(pod_anti_affinity,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: '容器',key: 'containers',                    
                    render : (h,params) => {
                        let containers  = params.row.containers
                        if(containers == null){
                            return h('p','无')
                        }else{
                            let value = JSON.stringify(containers,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: '主机网络',key: 'host_network'
                },
                {
                    title: '创建时间',key: 'create_time'
                },

            ],
           // 分页
            total_list: [],
            show_list: [],
            total: 0,
            pageSize: 15,
        }
    },
    methods: {
        btn_refresh(){
            this.refresh()
        },
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        refresh() {
            k8s_namespace_post(get_daemonset_list).then( (response) => {
                this.total_list = response.data
                this.total = response.data.length
                if(this.total < this.pageSize) {
                    this.show_list = this.total_list
                }else {
                    let currentPage = this.$refs.page.currentPage
                    let _start = (currentPage-1) * this.pageSize
                    let _end = currentPage * this.pageSize
                    this.show_list = this.total_list.slice(_start,_end)
                }
            })
        }
    },
    mounted() {
        this.refresh()
    },

}
</script>
<style scoped>

</style>